package cn.tsmchina.one.controller.index;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.tsmchina.one.domain.category.Category;
import cn.tsmchina.one.domain.config.Config;
import cn.tsmchina.one.domain.user.User;
import cn.tsmchina.one.pojo.ActivityPojo;
import cn.tsmchina.one.pojo.RestaurantPojo;
import cn.tsmchina.one.service.activity.ActivityService;
import cn.tsmchina.one.service.category.CategoryService;
import cn.tsmchina.one.service.config.ConfigService;
import cn.tsmchina.one.service.user.CustomerService;
import cn.tsmchina.one.service.user.UserService;
import cn.tsmchina.one.util.C;
import cn.tsmchina.one.util.JsonUtil;
import cn.tsmchina.one.util.LoginUtil;

@Controller
public class Index
{
	@Autowired
	private UserService userService;
	
	@Autowired
	private ActivityService activityService;
	
	@Autowired
	private ConfigService configService;
	
	@Autowired
	private CategoryService categoryService;
	@Autowired
	private CustomerService customerService;
	/**
	 * 消息提示
	 * @return
	 */
	@RequestMapping("/msg")
	public String msg()
	{
		return "msg";
	}
	
	/**
	 * 消息提示
	 * added by hy
	 * @return
	 */
	@RequestMapping("/listActivities")
	public String listActivities(Model model, HttpServletRequest request)
	{
		long cid = LoginUtil.getCustomerId(request, customerService);
		if(cid==-1) {
			return "m/login";
		}
		List<ActivityPojo> alist = activityService.getActivities(1, cid);
		if(alist.size()==0)
			return "redirect:/index?cid="+request.getAttribute("loginId");
		model.addAttribute("actPojos", alist);
		return "m/listActivities";
	}
	
	/**
	 * 首页
	 * @return
	 */
	@RequestMapping(value={"index","index.html"})
	public String index(Model model, HttpServletRequest request)
	{
//		Customer customer = (Customer) request.getSession().getAttribute("customer");
		String frontImage = null;
		String restaurantImage = null;
		String memberImage = null;
//		if(null!=customer){
			Long customerId = LoginUtil.getCustomerId(request, customerService);
			if(customerId!=-1) {
				//首页活动图片
				Config config = configService.getConfig(customerId, C.ConfigKeys.FRONT_ACTIVITY);
				frontImage = C.DefaultConfig.DEFAULT_FRONT_ACTIVITY_IMAGE;
				if(config!=null) {
					frontImage = config.getConfigValue();
				}
				model.addAttribute(C.ConfigKeys.FRONT_ACTIVITY, frontImage);
				
				//首页店铺图片
				config = configService.getConfig(customerId, C.ConfigKeys.FRONT_RESTAURANT);
				restaurantImage = C.DefaultConfig.DEFAULT_FRONT_RESTAURANT_IMAGE;
				if(config!=null) {
					restaurantImage = config.getConfigValue();
				}
				model.addAttribute(C.ConfigKeys.FRONT_RESTAURANT, restaurantImage);
				
				//首页会员图片
				config = configService.getConfig(customerId, C.ConfigKeys.FRONT_MEMBER);
				memberImage = C.DefaultConfig.DEFAULT_FRONT_MEMBER_IMAGE;
				if(config!=null) {
					memberImage = config.getConfigValue();
				}
				model.addAttribute(C.ConfigKeys.FRONT_MEMBER, memberImage);
				
				//首页标题
				config = configService.getConfig(customerId, C.ConfigKeys.FRONT_TITLE);
				memberImage = C.DefaultConfig.DEFAULT_FRONT_TITLE;
				if(config!=null) {
					memberImage = config.getConfigValue();
				}
				model.addAttribute(C.ConfigKeys.FRONT_TITLE, memberImage);
				
			} else {
				model.addAttribute(C.ConfigKeys.FRONT_ACTIVITY, C.DefaultConfig.DEFAULT_FRONT_ACTIVITY_IMAGE);
				model.addAttribute(C.ConfigKeys.FRONT_RESTAURANT, C.DefaultConfig.DEFAULT_FRONT_RESTAURANT_IMAGE);
				model.addAttribute(C.ConfigKeys.FRONT_MEMBER, C.DefaultConfig.DEFAULT_FRONT_MEMBER_IMAGE);
				model.addAttribute(C.ConfigKeys.FRONT_TITLE, C.DefaultConfig.DEFAULT_FRONT_TITLE);
			}
		
			
			Cookie[] cookies = request.getCookies();  
	        String[] cooks = null;  
	        long cid = 0;  
	        String password = null;
	        String phone = null;
	        String id = null;
	        if (cookies != null) {  
	            for (Cookie coo : cookies) {  
	                String aa = coo.getValue();  
	                //存储时候个数如下
					//Cookie cookie = new Cookie("user",phone+"==="+password+"==="+cid+"==="+u.getId());  
	                cooks = aa.split("===");  
	                if (cooks.length == 4) {  
	                    cid = Long.parseLong(cooks[2].toString());  
	                    password = cooks[1];  
	                    phone = cooks[0];
	                    id = cooks[3];
		                User u = userService.loginValid(phone,password, cid);
		    			if(null != u){
		    				request.getSession().setAttribute("user", u);
		    				request.getSession().setAttribute("userId", u.getId());
		    				
		    				String realName = u.getRealName();
		    				if(StringUtils.isEmpty(realName)||"null".equals(realName)){
		    					realName = "";
		    				}
		    				request.getSession().setAttribute("realName",realName);
		    				request.getSession().setAttribute("phone", u.getPhone());
		    				request.getSession().setAttribute("address", u.getAddress());
		    			
		    			}
	                }
	            }
	        } 
	            
		return "m/index";
	}
	
	/**
	 * 店铺列表信息查询
	 * @param model
	 * @return
	 */
	@RequestMapping("/category")
	public String activity(Model model, HttpServletRequest request)
	{
		long cid = LoginUtil.getCustomerId(request, customerService);
		List<Category> restaurants = categoryService.getAllSubRestaurant(cid);
		restaurants(restaurants, model);
		Object rid = request.getSession().getAttribute("currentRestaurantId");
		if(rid==null)
			rid="1";
		request.setAttribute("currentRestaurantId", /*request.getSession().getAttribute("currentRestaurantId")*/rid.toString());//进入店铺列表页面时需要标志之前进入的店铺id
		return "m/listRestaurants";
	}
	

	
	
	private void restaurants(List<Category> restaurants, Model model)
	{
		List<RestaurantPojo> pojos = new ArrayList<RestaurantPojo>();
		for (Category category : restaurants) {
			RestaurantPojo restaurantPojo = JsonUtil.parseObject(RestaurantPojo.class, category.getParams());
			restaurantPojo.setCid(category.getCid());
			restaurantPojo.setId(category.getId());
			pojos.add(restaurantPojo);
		}
		model.addAttribute("restaurants", pojos);
	}
}
